package org.ccay.excel.meta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.ccay.core.exception.ApplicationException;
import org.ccay.excel.exception.ExcelApplicationException;
import org.ccay.excel.importer.ExcelImportConstants;

/**
 * Excel Sheet的元数据信息。
 * 	
 * @author chaowangbang
 * @since Mar 16, 2012
 */
public class SheetMeta extends NamedElement implements java.lang.Cloneable{
	
	/**
	 * 标题行的索引号
	 */
	private int titleRowIndex;
	
	/**
	 * 数据起始行的索引号
	 */
	private int dataRowIndex;
	
	/**
	 * 数据结束行的索引号
	 */
	private int endDataRowIndex;
	
	/**
	 * 行数据对应的VO名称
	 */
	private String voBeanName;
	
	/**
	 * Sheet的数据消费者对应的Spring Bean
	 */
	private String consumerBean;
	
	/***
	 * Sheet行宽度
	 */
	private int exportColumnWidth; 
	
	/***
	 * Sheet列高度
	 */
	private int exportRowHeight;
	
	/**
	 * Sheet错误回写列
	 */
	private String errorDisplayRow;
	
	
	/**
	 * 获取错误信息回写列
	 * @return
	 */
	public String getErrorDisplayRow() {
		return errorDisplayRow;
	}


	/**
	 * 设置错误信息回写列
	 * @param errorDisplayRow
	 */
	public void setErrorDisplayRow(String errorDisplayRow) {
		this.errorDisplayRow = errorDisplayRow;
	}


	/***
	 * 获取导出列宽
	 * @return
	 */
	public int getExportColumnWidth() {
		return exportColumnWidth;
	}


	/**
	 * 设置导出列宽
	 * @param exportColumnWidth
	 */
	public void setExportColumnWidth(int exportColumnWidth) {
		this.exportColumnWidth = exportColumnWidth;
	}


	/***
	 * 获取到处列高度
	 * @return
	 */
	public int getExportRowHeight() {
		return exportRowHeight;
	}


	/**
	 * 设置导出列高度
	 * @param exportRowHeight
	 */
	public void setExportRowHeight(int exportRowHeight) {
		this.exportRowHeight = exportRowHeight;
	}
	/**
	 * AS扩展使用
	 */
	private String extendedConsumer;
	
	public String getExtendedConsumer() {
		return extendedConsumer;
	}



	public void setExtendedConsumer(String extendedConsumer) {
		this.extendedConsumer = extendedConsumer;
	}

	/**
	 * 当前sheetMeta是否改变
	 */
	private boolean change=false;
	
	/**
	 * 设置列去掉空格
	 */
	private String columnWipe;
	
	/**
	 * 列集合
	 */
	private List<ColumnMeta> columns = new ArrayList<ColumnMeta>();
	
	/**
	 * 添加Column元数据异常
	 */
	private static final String ADDCOLUMN_EXCEPTION = "ccay.excel.00080004";
	
	private int batchSize;
	
	/**
	 * 动态可配项
	 */
	private String dynamicItem;
	
	public String getDynamicItem() {
		return dynamicItem;
	}



	public void setDynamicItem(String dynamicItem) {
		this.dynamicItem = dynamicItem;
	}



	/**
	 * @param sheetName
	 */
	public SheetMeta(String sheetName) {
		super(sheetName);
	}
	


	public boolean isChange() {
		return change;
	}



	public void setChange(boolean change) {
		this.change = change;
	}



	/** 获取Sheet的名称
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getSheetName() {
		return getName();
	}
	
	/**
	 * 获取同一sheet 多块的时候  真正的name 代办使用同一个sheet
	 * @return
	 * Created：2012-9-11
	 * @author chaowangbang
	 */
	public String getPartSheetName(){
		if(getName().indexOf(ExcelImportConstants.EXCEL_SHEET_BLOCK_SPLIT)>-1){
			return getName().substring(0, getName().indexOf(ExcelImportConstants.EXCEL_SHEET_BLOCK_SPLIT));
		}
		return getName();
	}
	
	public String getExcelSheetName(){
		return getPartSheetName();
	}
	
	/** 添加一个列定义	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param column
	 * @throws ExcelApplicationException 
	 */
	public void addColumnMeta(ColumnMeta column) throws ApplicationException {
		//判断是否有重复的column
		for(ColumnMeta columnmeta:this.columns){
			if(column.equals(columnmeta)){
				throw new ExcelApplicationException(ADDCOLUMN_EXCEPTION,column.getName());
			}			
		}
		this.columns.add(column);
	}
	
	/** 获取列定义集合	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public List<ColumnMeta> getColumns() {
		return this.columns;
	}
	
	/** 设置列定义集合	 提供给动态列使用的
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public void setColumns(List<ColumnMeta> columns) {
		 this.columns =columns ;
	}
	
	/** 获得指定名称的列	
	 * @author chaowangbang
	 * @since Mar 20, 2012
	 * @param name
	 * @return
	 */
	public ColumnMeta getColumnByName(String name) {
		Iterator<ColumnMeta> iter = this.columns.iterator();
		while(iter.hasNext()) {
			ColumnMeta col = iter.next();
			if(col.getName().equalsIgnoreCase(name)) {
				return col;
			}
		}
		
		return null;
	}
	
	/** 根据显示名称获得指定名称的列	
	 * @author chaowangbang
	 * @since Mar 20, 2012
	 * @param name
	 * @return
	 */
	public ColumnMeta getColumnByDisplayName(String displayname) {
		Iterator<ColumnMeta> iter = this.columns.iterator();
		while(iter.hasNext()) {
			ColumnMeta col = iter.next();
			if(col.isCurrent(displayname)) {
				return col;
			}
		}
		
		return null;
	}

	/** 获取数据消费者Bean ID	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getConsumerBean() {
		return consumerBean;
	}

	/** 设置数据消费者的Bean ID	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param consumerBean
	 */
	public void setConsumerBean(String consumerBean) {
		this.consumerBean = consumerBean;
	}

	/** 获取数据起始行的索引号	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public int getDataRowIndex() {
		return dataRowIndex;
	}

	/** 设置数据起始行的索引号	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param dataRowIndex
	 */
	public void setDataRowIndex(int dataRowIndex) {
		this.dataRowIndex = dataRowIndex;
	}

	/** 获取标题行的索引号	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public int getTitleRowIndex() {
		return titleRowIndex;
	}

	/** 设置标题行的索引号
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param titleRowIndex
	 */
	public void setTitleRowIndex(int titleRowIndex) {
		this.titleRowIndex = titleRowIndex;
	}

	/** 获取VO的类名称	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getVoBeanName() {
		return voBeanName;
	}

	/** 设置VO的类名称	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param voBeanName
	 */
	public void setVoBeanName(String voBeanName) {
		this.voBeanName = voBeanName;
	}
	
	/**
	 * 得到列的验证器列表	
	 * @author chaowangbang
	 * @since 2013-6-25
	 * @param colName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ValidatorMeta> getValidatorMetas(String colName){
		ColumnMeta columnMeta = this.getColumnByName(colName);
		if(columnMeta == null) {
			return Collections.EMPTY_LIST;
		}
		return columnMeta.getValidators();
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.meta.NamedElement#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.meta.NamedElement#hashCode()
	 */
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	public int getBatchSize() {
		return batchSize;
	}

	public void setBatchSize(int batchSize) {
		this.batchSize = batchSize;
	}
	public int getEndDataRowIndex() {
		return endDataRowIndex;
	}

	public void setEndDataRowIndex(int endDataRowIndex) {
		this.endDataRowIndex = endDataRowIndex;
	}
	
	public String getColumnWipe() {
		return columnWipe;
	}

	public void setColumnWipe(String columnWipe) {
		this.columnWipe = columnWipe;
	}

	public SheetMeta copy() throws CloneNotSupportedException{
		SheetMeta copySheetMeta = (SheetMeta) this.clone();
		List<ColumnMeta>  copyColumnMetas =  new ArrayList<ColumnMeta>();
		for(ColumnMeta meta:this.getColumns()){
			copyColumnMetas.add(meta.copy());
		}
		copySheetMeta.setChange(true);
		copySheetMeta.setColumns(copyColumnMetas);
		return  copySheetMeta;
	}
}
